const { get } = require('mongoose');
const adminsService = require('../../dao/admins/adminsDao.js');
const { getAccessToPag } = require('../userServices.js');

module.exports = {
    // 显示所有数据
    async getallInfo(){
        return await adminsService.getAll()
    },
    //登录
    async login(adminName,adminPassword){
        return await adminsService.getadminsByNameAndPwd(adminName,adminPassword)
    },
    //注册
    async reg(adminName,adminPassword,photo){
        let result =  await adminsService.getadminsByName(adminName);
        console.log(result);
        if(result){
            return {"code":0,"msg":"注册失败，该用户已经存在"}
        }else{
            await adminsService.insertadmins(adminName,adminPassword,photo)
            return  {"code":1,"msg":"注册成功"}
        }
    },
    // 查找数据
    async chazhao(_id){
        return await adminsService.getadminsById(_id);
    },
    //删除数据
    async deleteAdminsInfo(adminsId){
        return await adminsService.deleAdminsById(adminsId);
    },
    // 修改admins信息
    async updateAdminsInfo({
        _id,
        adminName,
        adminPassword,
        photo
    }){
        return await adminsService.updateadminsById({
            _id,
            adminName,
            adminPassword,
            photo
        })
    },
    // 分页
    async getAccessToPag({
        currentpage,
        pagesize
    }){
         //创建一个空对象
         let pagedata = {};
         //当前页数
         pagedata.currentpage = currentpage;
         //每页显示的条数
         pagedata.pagesize = pagesize;
         //显示的数据
         pagedata.data = await adminsService.accessToPag({
            currentpage,
            pagesize
        });
        //总条数
        pagedata.totalInt = await adminsService.getsNumber();
        //总页数
        pagedata.totalSize = Math.ceil(pagedata.totalInt / pagesize);
        return pagedata
    }
}